#ifndef DECLARATION_HPP
#define DECLARATION_HPP

#include <fstream>

using namespace std;

class Declaration
{
    public:
        Declaration();
//        virtual ~Declaration();

        bool IsValid(ifstream &fl);
        string SetDec();

    private:
        string version;
        string encoding;
};

Declaration::Declaration(){
    version = encoding = "";
}
/*
Declaration::~Declaration(){

}
*/
bool Declaration::IsValid(ifstream &fl){
    string dec;
    getline(fl,dec,'\n');

    //cout << dec << endl;

    string xml;
    int sp = dec.find(' ');
    xml = dec.substr(0,sp);
    dec = dec.substr(sp+1);

    //cout << dec << endl;

    if(xml != "<?xml") return false;

    string vers, enc;
    int eq = dec.find('=');
    vers = dec.substr(0,eq);
    dec = dec.substr(eq+2);

    //cout << dec << endl;

    if(vers != "version") return false;

    int vp = dec.find('\"');
    version = dec.substr(0,vp);
    dec = dec.substr(vp+2);

    //cout << dec << endl;

    if(version != "1.0" && version != "1.1") return false;

    int en = dec.find('=');
    enc = dec.substr(0,en);
    dec = dec.substr(en+2);

    //cout << dec << endl;

    if(enc != "encoding") return false;

    vp = dec.find('\"');
    encoding = dec.substr(0,vp);
    dec = dec.substr(vp+1);

    //cout << dec << endl;

    if(dec != "?>") return false;

    //cout << xml << " " << vers << "=\"" << version << "\" " << enc << "=\"" << encoding << "\"" << endl;

	return true;
}

string Declaration::SetDec(){
    string dec;
    if(version != "" && encoding != "") dec = "<?xml version=\"" + version + "\" encoding=\"" + encoding + "\"?>";
    else                                dec = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

    return dec;
}

#endif
